/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [FileMenuListener.java]
 * Creation Date        [11-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_gui.notepad_V1.ui.file;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

import pkg_02_gui.notepad_V1.ui.main.NotepadFrame;

/**
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class FileMenuListener extends FileMenu implements ActionListener {

	private static final long serialVersionUID = -7617320726152131259L;

	private JTextArea textArea = null; // 文本框
	private JLabel statusLabel = null; // 状态栏
	private File file = null; // 文件
	private boolean isNewFile = true; // 是否为新文件

	private final FileUtil fileUtil;
	private FileMenuToolBar fileToolBar;
	private NotepadFrame frame = null;

	/**
	 * 为菜单项添加监听器
	 */
	private void addListenerToItem() {
		getNewFileItem().addActionListener(this);
		getOpenFileItem().addActionListener(this);
		getSaveItem().addActionListener(this);
		getSaveAsItem().addActionListener(this);
		getPageSettingItem().addActionListener(this);
		getPrintItem().addActionListener(this);
		getExitItem().addActionListener(this);
		fileToolBar = new FileMenuToolBar(this);
	}

	/**
	 * 添加浮动工具栏到窗口
	 */
	private void addToolbarToPanel() {
		frame.getToolbarPanel().add(fileToolBar);
	}

	public FileMenuListener(String menuName, JTextArea textArea,
			JLabel statusLabel, File file, NotepadFrame frame) {
		super(menuName);
		addListenerToItem();
		fileUtil = new FileUtil();
		this.textArea = textArea;
		this.statusLabel = statusLabel;
		this.file = file;
		this.frame = frame;
		addToolbarToPanel();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		/** **************************新建文件*************************** */
		if (e.getSource() == getNewFileItem()
				|| e.getSource() == fileToolBar.getNewButton()) {
			textArea.requestFocusInWindow();
			Object[] options = { "确定", "取消" };
			int result = JOptionPane.showOptionDialog(null, "请保存文档, 按确定新建文件",
					"警告", JOptionPane.DEFAULT_OPTION,
					JOptionPane.WARNING_MESSAGE, null, options, options[0]);
			if (result == JOptionPane.YES_NO_OPTION) {
				textArea.setText("");
			}
			statusLabel.setText("空白文档");
			file = null;
		}
		/** **************************打开文件*************************** */
		if (e.getSource() == getOpenFileItem()
				|| e.getSource() == fileToolBar.getOpenButton()) {
			textArea.requestFocusInWindow();
			JFileChooser chooser = new JFileChooser();
			if (file != null) {
				chooser.setSelectedFile(file);
			}
			// 设置文件过滤器
			chooser.setFileFilter(new FileNameExtensionFilter("文件选项",
					new String[] { "txt", "sql", "java" }));
			int returnValue = chooser.showOpenDialog(textArea.getParent());
			if (returnValue == JFileChooser.APPROVE_OPTION) {
				file = chooser.getSelectedFile();
				fileUtil.openFile(textArea, file);
				isNewFile = false;// 文件为已保存的
				statusLabel.setText("当前打开文件:" + file.getAbsolutePath() + " 行:"
						+ textArea.getRows());
				textArea.setCaretPosition(0);// 设置光标的位置在开始
			}
		}
		/** **************************保存文件*************************** */
		if (e.getSource() == getSaveItem()
				|| e.getSource() == fileToolBar.getSaveButton()) {
			textArea.requestFocusInWindow();
			fileUtil.saveFile(textArea, isNewFile, file, statusLabel);
		}
		/** **************************另存文件*************************** */
		if (e.getSource() == getSaveAsItem()) {
			textArea.requestFocusInWindow();
			isNewFile = true;
			fileUtil.saveFile(textArea, isNewFile, file, statusLabel);
		}
		/** **************************页面设置*************************** */
		if (e.getSource() == getPageSettingItem()) {
			textArea.requestFocusInWindow();
		}
		/** **************************打印文件*************************** */
		if (e.getSource() == getPrintItem()) {
			textArea.requestFocusInWindow();
			try {
				textArea.print();
			} catch (PrinterException e1) {
				e1.printStackTrace();
			}
		}
		/** **************************退出系统*************************** */
		if (e.getSource() == getExitItem()) {
			textArea.requestFocusInWindow();
			Object[] options = { "确定", "取消" };
			int s = JOptionPane.showOptionDialog(null, "请注意保存文档! 按\"确定\"退出",
					"警告!", JOptionPane.DEFAULT_OPTION,
					JOptionPane.WARNING_MESSAGE, null, options, options[0]);
			if (s == JOptionPane.YES_OPTION) {
				System.exit(0);
			}
		}
	}
}
